using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITS.DAL.Events;
using ITS.Core.Domain.Events;

namespace ITS.DAL.Concrete.EF.Events
{
    public class EFEventRepository : IEventRepository
    {
        private readonly EnterpriseContext _context = new EnterpriseContext();

        public IQueryable<Event> GetAllActiveEvents()
        {
            return _context.Events;
        }

        public IQueryable<Event> GetNextEvents()
        {
            return _context.Events.Where(x => (x.StartDate >= DateTime.Now || x.IsPermanent == true) && x.Active == true);
        }

        public IQueryable<Event> GetPreviousEvents()
        {
            return _context.Events.Where(x => x.StartDate < DateTime.Now && x.Active == true && x.IsPermanent == false);
        }

        public IQueryable<EventCategory> GetAllActiveCategories()
        {
            return _context.EventCategories;
        }

        public Event Get(Guid eventID)
        {
            return _context.Events.FirstOrDefault(x => x.EventID == eventID);
        }
        public void Save(Event o)
        {
            if (o.EventID.Equals(Guid.Empty))
            {
                o.EventID = Guid.NewGuid();
                _context.Events.Add(o);
            }
            else
            {
                _context.Entry(o).State = System.Data.EntityState.Modified;
            }
            _context.SaveChanges();
        }
        public void AddEvent(Event eventItem)
        {
            eventItem.EventID = Guid.NewGuid();
            _context.Events.Add(eventItem);
            Save();
        }
        public void Save()
        {
            _context.SaveChanges();
        }
    }
}
